package com.tz.controller;


import com.tz.entity.User;
import com.tz.mapper.UserMapper;
import com.tz.service.UserService;
import com.tz.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.从前端接收用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.调用service查询
        User user = userService.login(username, password);
//        request.setAttribute("user");
        
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        //3.判断user是否为空
        if(user!=null){
            writer.write("登录成功");
//            String contextPath = request.getContextPath();
//            response.sendRedirect(contextPath+"/index.html");

            //登录成功
        }else {
            writer.write("登录失败");
//            request.setAttribute("username",username);
////            request.setAttribute("password",password);
//            request.getRequestDispatcher("/login.jsp").forward(request,response);
            //登录失败
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
